<template>
  <el-main>
    <!-- 模糊查询 -->
    <el-form
      :model="parms"
      ref="searchForm"
      label-width="80px"
      :inline="true"
      size="small"
    >
      <el-form-item label="问卷标题">
        <el-input v-model="parms.questionTitle"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button style="color: #ff7670" @click="resetBtn" icon="el-icon-close"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 表格列表 -->
    <el-table :height="tableHeight" :data="tableList" border stripe>
      <el-table-column prop="questionImg" width="80" label="问卷图片" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px; border-radius: 50%"
            :src="scope.row.questionImg"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="questionTitle" label="问卷标题"></el-table-column>
      <el-table-column prop="questionDesc" label="问卷描述"></el-table-column>
      <el-table-column prop="questionOrder" label="问卷序号"></el-table-column>
      <el-table-column prop="questionStatus" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.questionStatus == '0'" type="danger" size="normal"
            >关闭</el-tag
          >
          <el-tag v-if="scope.row.questionStatus == '1'" type="success" size="normal"
            >调查中</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="lookBtn(scope.row)"
            >查看票数</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 
    @size-change 页容量改变时触发的事件
    @current-change 页数改变时触发的事件
    :current-page  当前第几页 
    :page-sizes 
    :page-size  没有查询几条
     :total 总条数
    -->
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page.sync="parms.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="parms.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="parms.total"
      background
    >
    </el-pagination>
    <!-- 弹框 -->
    <sys-dialog
      :title="addDialog.title"
      :height="addDialog.height"
      :width="addDialog.width"
      :visible="addDialog.visible"
      :print="addDialog.print"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <template slot="content">
        <div id="printTotal">
          <div
            style="
              text-align: center;
              font-size: 21px;
              font-weight: 600;
              padding: 10px 0px;
            "
          >
            {{ questionTitle }}
          </div>
          <table style="width: 100%" border="1">
            <thead>
              <th>题目</th>
              <th>选项</th>
              <th>票数</th>
            </thead>
            <tbody>
              <template v-for="item in lookTableList">
                <tr v-for="(m, i) in item.paperChoice" :key="m.chioceId">
                  <td style="width:40%;text-align:center;" v-if='i == 0' :rowspan="item.paperChoice.length">{{item.paperTitle}}</td>
                  <td>{{m.choiceText}}</td>
                  <td>{{m.total}}</td>
                </tr>
              </template>
            </tbody>
          </table>
        </div>
      </template>
    </sys-dialog>
  </el-main>
</template>

<script>
import { getListApi, getTotalListApi } from "@/api/question";
import SysDialog from "@/components/system/SysDialog.vue";
export default {
  //注册组件
  components: {
    SysDialog,
  },
  data() {
    return {
      questionTitle: "",
      //统计表格的数据
      lookTableList: [],
      addDialog: {
        title: "票数统计",
        height: 500,
        width: 950,
        visible: false,
        print:true
      },
      //表格的高度
      tableHeight: 0,
      //表格数据
      tableList: [],
      //列表参数
      parms: {
        currentPage: 1,
        pageSize: 10,
        questionTitle: "",
        total: 0, //分页用的总条数
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 200;
    });
  },
  methods: {
    //弹框确认
    onConfirm() {
      this.addDialog.visible = false;
    },
    //弹框的关闭
    onClose() {
      this.addDialog.visible = false;
    },
    //查看票数
    async lookBtn(row) {
      //查询统计数据
      let res = await getTotalListApi({ questionId: row.questionId });
      if (res && res.code == 200) {
        console.log(res.data.listPaper);
        this.lookTableList = res.data.listPaper;
        this.questionTitle = res.data.questionTitle;
      }
      this.addDialog.visible = true;
    },
    //重置按钮
    resetBtn() {
      this.parms.questionTitle = "";
      this.getList();
    },
    //搜索按钮
    searchBtn() {
      this.getList();
    },
    //页数改变时触发
    currentChange(val) {
      this.parms.currentPage = val;
      this.getList();
    },
    //页容量改变时触发
    sizeChange(val) {
      this.parms.pageSize = val;
      this.parms.currentPage = 1;
      this.getList();
    },
    //获取列表
    async getList() {
      let res = await getListApi(this.parms);
      if (res && res.code == 200) {
        //给表格数据赋值
        this.tableList = res.data.records;
        //分页总条数
        this.parms.total = res.data.total;
      }
      console.log(res);
    },
  },
};
</script>

<style lang="scss" scoped>
table {
  // width: 300px;
  text-align: center;
  border-collapse: collapse; /* 为table设置这个属性 */
}
</style>
